package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.form.CourseInfoForm;
import com.atguigu.guli.service.edu.entity.vo.CourseInfoVo;
import com.atguigu.guli.service.edu.service.CourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-06-24
 */
@CrossOrigin
@RestController
@RequestMapping("/admin/edu/course")
public class AdminCourseController {

    @Autowired
    private CourseService courseService;

    // 保存课程信息
    @ApiOperation(value = "保存课程信息")
    @PostMapping("saveCourseInfo")
    public R saveCourseInfo(@RequestBody CourseInfoForm courseInfo) {
        String courseId = courseService.saveCourseInfo(courseInfo);
        return R.ok().data("id", courseId);
    }

    // 查询课程信息
    @ApiOperation(value = "查询单个课程信息")
    @GetMapping("getCourseInfo")
    public R getCourseInfo(@RequestParam String courseId) {
        CourseInfoForm course = courseService.getCourseInfo(courseId);
        return R.ok().data("item", course);
    }

    // 分页查询课程列表
    @ApiOperation(value = "分页查询课程列表")
    @GetMapping("list/{pageNum}/{pageSize}")
    public R list(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {

        // 分页查询, 查询 courseInfoVo 数据, 涉及到多表联查,需要自定义方法
        Page<CourseInfoVo> page = courseService.getCourseInfoVos(pageNum, pageSize);
        return R.ok().data("page", page);
    }

    // 删除课程
    @ApiOperation(value = "删除课程")
    @DeleteMapping("delCourse/{id}")
    public R delete(@PathVariable String id) {
        courseService.removeById(id);
        return R.ok().message("删除课程成功");
    }

    // 更新课程信息
    @ApiOperation(value = "更新课程")
    @PutMapping("updateCourse/{id}")
    public R updateCourse(@PathVariable("id") String id, @RequestBody CourseInfoForm courseInfo) {
        courseService.updateCourse(id, courseInfo);
        System.out.println("id: " + id);
        System.out.println("更新数据: " + courseInfo);
        return R.ok();
    }
}

